<template>
    <div class="index-lists">
        <!-- <el-card class="!border-none" shadow="never"> -->
        <!-- <el-form
        ref="formRef"
        class="mb-[-16px]"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item label="文件描述" prop="fileDescriptor">
          <el-select
            v-model="queryParams.fileDescriptor"
            class="w-[280px]"
            clearable
          >
            <el-option label="官方来文" value="1" />
            <el-option label="递交官方" value="2" />
            <el-option label="客户来文" value="3" />
            <el-option label="本所文件" value="4" />
            <el-option label="国防官文" value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="文件名称" prop="fileName">
          <el-input class="w-[280px]" v-model="queryParams.fileName" />
        </el-form-item>
        <el-form-item label="客户名称" prop="accountName">
          <el-input class="w-[280px]" v-model="queryParams.accountName" />
        </el-form-item>
        <el-form-item label="上传者" prop="uploaderid">
          <el-select
            v-model="queryParams.uploaderid"
            class="w-[280px]"
            clearable
          >
            <el-option label="张三" value="1" />
            <el-option label="李四" value="2" />
            <el-option label="王五" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="上传时间" prop="uploadTime">
          <daterange-picker
            v-model:startTime="queryParams.uploadTimeStart"
            v-model:endTime="queryParams.uploadTimeEnd"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form> -->
        <!-- </el-card> -->
        <!-- <el-card class="!border-none mt-4" shadow="never"> -->
        <div>
            <el-button v-perms="['file:add']" type="primary" @click="selectFile = true">
                选择文件
            </el-button>
            <el-button v-perms="['file:add']" type="primary" @click="handleAdd()">
                上传文件
            </el-button>
        </div>
        <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
            <el-table-column type="selection" width="55" />
            <el-table-column label="ID" prop="id" min-width="50" />
            <el-table-column label="文件描述" prop="fileDescriptor" min-width="130" sortable />
            <el-table-column label="文件名称" prop="fileName" min-width="130" sortable />
            <el-table-column label="客户名称" prop="accountName" min-width="130" sortable>
            </el-table-column>
            <el-table-column label="上传者" prop="uploader" min-width="130" sortable />
            <el-table-column label="上传时间" prop="uploadTime" min-width="180" sortable />
            <el-table-column label="文件类型" prop="fileType" min-width="130" sortable />
            <el-table-column label="操作人" prop="userName" min-width="130" sortable />
            <el-table-column label="创建时间" prop="createTime" min-width="180" sortable />
            <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ row }">
                    <el-button v-perms="['file:edit']" type="primary" link @click="handleEdit(row)">
                        编辑
                    </el-button>
                    <el-button
                        v-perms="['file:del']"
                        type="danger"
                        link
                        @click="handleDelete(row.id)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="flex justify-end mt-4">
            <pagination v-model="pager" @change="getLists" />
        </div>
        <!-- </el-card> -->
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
        <el-dialog v-model="selectFile" title="选择文件" width="800px">
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
                <el-table-column type="selection" width="55" />
                <el-table-column label="ID" prop="id" min-width="50" />
                <el-table-column label="附件名称" prop="fileDescriptor" min-width="100" sortable />
                <el-table-column label="文件类型" prop="fileName" min-width="100" sortable />
                <el-table-column label="文件描述" prop="accountName" min-width="100" sortable />
                <el-table-column label="上传者" prop="uploader" min-width="100" sortable />
                <el-table-column label="上传时间" prop="uploadTime" min-width="100" sortable />
            </el-table>
            <template #footer>
                <div class="dialog-footer">
                    <el-button>取消</el-button>
                    <el-button type="primary"> 确认 </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup name="file">
import { fileDelete, fileLists } from '@/api/syfile'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    fileDescriptor: '',
    fileName: '',
    accountName: '',
    uploaderid: '',
    uploadTimeStart: '',
    uploadTimeEnd: ''
})
const selectFile = ref(false)

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: fileLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await fileDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
